package com.example.demo.service;


import com.example.demo.mapper.LogInfoMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public int addUser(UserInfo userInfo) {

        // 3s后执行addUser
        try {
            Thread.sleep(3000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userMapper.addUser(userInfo);
    }



    @Autowired
    private LogInfoMapper logInfoMapper;


//    事务3(实现事务的嵌套)
    @Transactional(propagation = Propagation.REQUIRED)
    public int addUser2(UserInfo userInfo) {
        int ret = userMapper.addUser(userInfo);
        System.out.println("添加用户： " + ret);
//        LogInfo logInfo = new LogInfo();
//        logInfo.setName("添加用户");
//        logInfo.setDesc("添加用户的结果： " + ret);
//        logInfoMapper.addLog(logInfo);
        return ret;
    }









    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int addUser5(UserInfo userInfo) {
        int ret = userMapper.addUser(userInfo);
        System.out.println("添加用户： " + ret);
//        LogInfo logInfo = new LogInfo();
//        logInfo.setName("添加用户");
//        logInfo.setDesc("添加用户的结果： " + ret);
//        logInfoMapper.addLog(logInfo);
        return ret;
    }
}
